import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/blind_model/list_by_blindId_model.dart';
import 'package:yysk_app/model/blind_model/open_blind_model.dart';
import 'package:yysk_app/pages/nft_pages/danation/danation_choose_platform.dart';
import 'package:yysk_app/pages/nft_pages/my_blind/my_blind_list_controller.dart';
import 'package:yysk_app/pages/nft_pages/my_blind/open_blind_record_page.dart';
import 'package:yysk_app/pages/nft_pages/my_prop/prop_batch_gift/prop_batch_gift_page.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/dialog_widget.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:yysk_app/widget/app_bar_back_widget.dart';
import 'package:yysk_app/widget/button_widget.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:oktoast/oktoast.dart';

class MyBlindListPage extends GetView<MyBlindListController> {
  static const String path = '/myBlindListPage';
  MyBlindListPage({super.key});

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;

    return Scaffold(
      body: Obx(() {
        return Container(
          color: AppColors.backgroundColor,
          child: Column(
            children: [
              Expanded(
                child: Stack(
                  children: [
                    SizedBox(
                      width: context.width,
                      child: ImageAssetWidget(
                        width: 375.px,
                        height: 240.px,
                        alignment: Alignment.topCenter,
                        imageUrl: 'static/images/box/box_bg.png',
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                      child: Column(
                        children: [
                          SizedBox(height: top),
                          CustomAppBar2(
                            title: controller.boxName,
                            context: context,
                          ),
                          // Row(
                          //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          //   crossAxisAlignment: CrossAxisAlignment.center,
                          //   children: [
                          //     AppBarBackWidget(
                          //       context: context,
                          //       onBack: () {},
                          //       backgroundColor: AppColors.backgroundColorBlack,
                          //     ),
                          //     Expanded(
                          //       child: Container(
                          //         // padding: EdgeInsets.only(left: 35.px),
                          //         child: Center(
                          //           child: Text(
                          //             controller.boxName,
                          //             style: TextStyle(
                          //               fontSize: 18.px,
                          //               color: AppColors.fontColorBlack,
                          //             ),
                          //           ),
                          //         ),
                          //       ),
                          //     ),
                          //     // GestureDetector(
                          //     //   onTap: () {
                          //     //     Get.toNamed(
                          //     //       '${OpenBlindRecordPage.path}?boxId=${controller.boxId}',
                          //     //       arguments: {'boxId': controller.boxId},
                          //     //     );
                          //     //   },
                          //     //   child: Container(
                          //     //     width: 77.px,
                          //     //     height: 28.px,
                          //     //     margin: EdgeInsets.only(right: 16.px),
                          //     //     decoration: BoxDecoration(
                          //     //       color: AppColors.backgroundColor,
                          //     //       borderRadius: BorderRadius.circular(364.px),
                          //     //     ),
                          //     //     child: Center(
                          //     //       child: Text(
                          //     //         '开盒记录',
                          //     //         style: TextStyle(
                          //     //           fontSize: 12.px,
                          //     //           color: AppColors.fontColorBlack,
                          //     //         ),
                          //     //       ),
                          //     //     ),
                          //     //   ),
                          //     // ),
                          //   ],
                          // ),
                          Padding(
                            padding: EdgeInsets.fromLTRB(0, 24.px, 0, 20.px),
                            child: Center(
                              child: Column(
                                children: [
                                  Text(
                                    '${controller.count.value}',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                      fontSize: 30.px,
                                      fontWeight: FontWeight.w500,
                                    ),
                                  ),
                                  Text(
                                    '(持有量)',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                      fontSize: 12.px,
                                      fontWeight: FontWeight.w400,
                                    ),
                                  ),
                                  hiSpace(height: 14.px),
                                  ButtonWidget(
                                    label: '开心愿盒',
                                    width: 110.px,
                                    height: 32.px,
                                    fontColor: AppColors.fontColorBlack,
                                    fontSize: 12.px,
                                    alignment: Alignment.center,
                                    onPressed: () {
                                      if (controller.whether == true) {
                                        _showCodeSheet(context);
                                      } else {
                                        showToast('暂无心愿盒可开启！');
                                      }
                                      // _showRewardSheet();
                                    },
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Expanded(
                            child: RefreshIndicator(
                              onRefresh: _handleRefresh,
                              child: Container(
                                padding: EdgeInsets.only(top: 31.px),
                                decoration: BoxDecoration(
                                  color: AppColors.backgroundColor,
                                  borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(20.px),
                                    topRight: Radius.circular(20.px),
                                  ),
                                ),
                                child: CustomScrollView(
                                  controller: controller.scrollController,
                                  physics: AlwaysScrollableScrollPhysics(),
                                  slivers: [
                                    SliverPadding(
                                      padding: EdgeInsets.fromLTRB(
                                        16.px,
                                        0,
                                        16.px,
                                        30.px,
                                      ),
                                      sliver: SliverGrid(
                                        gridDelegate:
                                            SliverGridDelegateWithFixedCrossAxisCount(
                                              crossAxisCount: 2,
                                              crossAxisSpacing: 18.px,
                                              mainAxisSpacing: 14.px,
                                              childAspectRatio: 165 / 214,
                                            ),
                                        delegate: SliverChildBuilderDelegate(
                                          childCount: controller.list.length,
                                          (BuildContext context, int index) {
                                            return LayoutBuilder(
                                              builder: (
                                                BuildContext context,
                                                BoxConstraints constraints,
                                              ) {
                                                return _listItem(
                                                  controller.list[index],
                                                );
                                              },
                                            );
                                          },
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      }),
      bottomNavigationBar: Obx(
        () =>
            controller.transferableStatus.value == true
                ? BottomAppBar(
                  color: Colors.white,
                  padding: EdgeInsets.symmetric(horizontal: 24.px, vertical: 0),
                  child: ButtonWidget(
                    height: 55.px,
                    width: 322.px,
                    fontSize: 22.px,
                    label: '批量转赠',
                    imageBg: 'static/images/btm.png',
                    contentType: ContentType.other,
                    onPressed: () {
                      Get.toNamed(
                        '${PropBatchGiftPage.path}?boxId=${controller.boxId}&type=1&transferType=2',
                        arguments: {
                          'boxId': controller.boxId,
                          'type': '1',
                          "transferType": '2',
                        },
                      );
                      //  Get.toNamed(DanationChooseGoodsPage.path,arguments: {'type': value});
                    },
                  ),
                )
                : SizedBox(),
      ),
    );
  }

  Widget _listItem(ListByBlindIdItem item) {
    return Container(
      width: Get.context!.width,
      margin: const EdgeInsets.only(bottom: 14),
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(
        color: AppColors.backgroundColorLightGrey,
        borderRadius: BorderRadius.circular(10.px),
        border: Border.all(color: Colors.white.withOpacity(0.12)),
        boxShadow: [
          BoxShadow(
            color: AppColors.backgroundColorBlack.withAlpha(25),
            offset: const Offset(0, 14),
            blurRadius: 24,
            spreadRadius: 0,
          ),
        ],
      ),
      child: Stack(
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Stack(
                children: [
                  LazyImage(
                    imageUrl: '${item.boxImageUrl}',
                    width: Get.context!.width,
                    boxFit: BoxFit.cover,
                    height: 165.px,
                    alignment: Alignment.topCenter,
                  ),

                  if (item.isLockedHouse == true)
                    Positioned(
                      left: 0.px,
                      top: 0.px,
                      right: 0,
                      bottom: 0,
                      child: Container(
                        padding: EdgeInsets.only(left: 14.px, right: 14.px),
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(0, 0, 0, 0.6),
                          borderRadius: BorderRadius.circular(6.px),
                        ),
                        child: Center(
                          child: Text(
                            '解锁时间 \n${item.lockedPositionTime}',
                            style: TextStyle(color: AppColors.fontColor),
                            textAlign: TextAlign.center,
                          ),
                        ),
                      ),
                    ),
                  if ((item.isLockedHouse == false &&
                      item.giveStatus == 'GIVING'))
                    Positioned(
                      left: 0.px,
                      top: 0.px,
                      right: 0,
                      bottom: 0,
                      child: Container(
                        padding: EdgeInsets.only(left: 14.px, right: 14.px),
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(0, 0, 0, 0.6),
                          borderRadius: BorderRadius.circular(6.px),
                        ),
                        child: Center(
                          child: Text(
                            '转赠中...',
                            style: TextStyle(color: AppColors.fontColor),
                            textAlign: TextAlign.center,
                          ),
                        ),
                      ),
                    ),
                ],
              ),
              Padding(
                padding: EdgeInsets.fromLTRB(7.px, 5.px, 10.px, 0),
                child: SizedBox(
                  width: Get.context!.width,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(4.px),
                            ),
                            child: Center(
                              child: Text(
                                '买入价',
                                style: TextStyle(
                                  color: AppColors.fontColorBlack,
                                  fontSize: 13.px,
                                ),
                                textAlign: TextAlign.center,
                              ),
                            ),
                          ),
                          Text(
                            '￥${item.buyPrice}',
                            style: TextStyle(
                              color: AppColors.fontColorBlack,
                              fontSize: 13.px,
                            ),
                            textAlign: TextAlign.left,
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          // 暂时隐藏转赠按钮
          if (item.isGive == true &&
              item.isLockedHouse == false &&
              item.giveStatus == 'NOT')
            Positioned(
              left: 8.px,
              top: 8.px,
              child: Container(
                height: 26.px,
                padding: EdgeInsets.only(left: 14.px, right: 14.px),
                decoration: BoxDecoration(
                  color: Color.fromRGBO(0, 0, 0, 0.5),
                  borderRadius: BorderRadius.circular(6.px),
                ),
                child: Center(
                  child: Text(
                    '可转赠',
                    style: TextStyle(color: AppColors.fontColor),
                  ),
                ),
              ),
            ),
        ],
      ),
    );
  }

  _showCodeSheet(BuildContext context) {
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: Color.fromARGB(0, 0, 0, 0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: ImageAssetWidget(
                      width: 29.px,
                      height: 28.px,
                      alignment: Alignment.topCenter,
                      imageUrl: 'static/images/close2_btm.png',
                    ),
                  ),
                ],
              ),
              Container(
                width: 312.px,
                height: 312.px,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('static/images/box/open_blind.png'),
                    fit: BoxFit.fitWidth,
                  ),
                ),
              ),
              hiSpace(height: 29.px),
              ButtonWidget(
                label: '开心愿盒',
                height: 55.px,
                width: 157.px,
                fontSize: 20.px,
                contentType: ContentType.other,
                colorStart: AppColors.fontColorYellowStart,
                colorEnd: AppColors.fontColorYellowEnd,
                imageBg: 'static/images/box/blind_btn.png',
                onPressed: () {
                  // 添加判断 是否持有的可以开心愿盒
                  controller.openBlind().then(
                    (res) => {
                      if (res?.successful)
                        {
                          Navigator.of(context).pop(),
                          controller.getBlindList(),
                          controller.getCount(),
                          _showRewardSheet(),
                        }
                      else
                        {Navigator.of(context).pop()},
                    },
                  );
                },
              ),
            ],
          ),
        );
      },
    );
  }

  _showRewardSheet() {
    var detail = controller.openBlindDetail.value;
    return showDialog(
      context: Get.context!,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: Color.fromARGB(0, 0, 0, 0),
          // backgroundColor: Colors.amber,
          insetPadding: EdgeInsets.all(0),
          child: Stack(
            children: [
              Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    padding: EdgeInsets.only(right: 35.px),
                    margin: EdgeInsets.only(bottom: 20.px),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        GestureDetector(
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                          child: ImageAssetWidget(
                            width: 29.px,
                            height: 28.px,
                            alignment: Alignment.topCenter,
                            imageUrl: 'static/images/close2_btm.png',
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    width: context.width,
                    height: 262.px,
                    decoration: BoxDecoration(
                      // color: AppColors.backgroundColorLightGreen,
                      image: DecorationImage(
                        image: AssetImage('static/images/box/reward_blind.png'),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  hiSpace(height: 39.px),
                  ButtonWidget(
                    label: detail?.whether == true ? '继续开盒' : '关闭',
                    height: 55.px,
                    width: 157.px,
                    fontSize: 20.px,
                    contentType: ContentType.other,
                    colorStart: AppColors.fontColorYellowStart,
                    colorEnd: AppColors.fontColorYellowEnd,
                    imageBg: 'static/images/box/blind_btn.png',
                    onPressed: () {
                      if (detail?.whether == false) {
                        Navigator.of(context).pop();
                      } else {
                        controller.openBlind().then(
                          (res) => {
                            if (res?.successful)
                              {
                                Navigator.of(context).pop(),
                                controller.getBlindList(),
                                controller.getCount(),
                                _showRewardSheet(),
                              }
                            else
                              {Navigator.of(context).pop()},
                          },
                        );
                      }
                    },
                  ),
                ],
              ),
              Positioned(
                width: context.width,
                top: 76.px,
                left: 0.px,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '恭喜获得',
                      style: TextStyle(
                        fontSize: 24.px,
                        color: AppColors.fontColor3D,
                        fontFamily: 'CustomFont',
                      ),
                    ),
                  ],
                ),
              ),
              Positioned(
                width: context.width,
                top: 120.px,
                left: 0.px,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    LazyImage(
                      width: 120.px,
                      height: 120.px,
                      borderRadius: 13.px,
                      alignment: Alignment.topCenter,
                      imageUrl: '${detail?.imageUrl}',
                    ),
                  ],
                ),
              ),
              Positioned(
                width: context.width,
                top: 254.px,
                left: 0.px,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      '${detail?.name}*${detail?.count}',
                      style: TextStyle(
                        fontSize: 14.px,
                        color: AppColors.fontColor3D,
                        fontWeight: FontWeight.w500,
                        fontFamily: 'CustomPINGFANG',
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Future<void> _handleRefresh() async {
    await controller.getBlindList();
    controller.getCount();

    return;
  }
}
